<?php

class pam_passport_site_basic {

    public function login($userData, $vcode = false, &$msg) {
        $userData = utils::_filter_input($userData); // 过滤xss攻击
        if ($vcode && ! $this->vcode_verify($vcode)) {
            $msg = app::get('pam')->_('验证码错误');
            return false;
        }
        $account = app::get('pam')->model('members')->getList('member_id', array(
            'login_account' => $userData['login_account']
        ));
        if (! $account) {
            $msg = app::get('pam')->_('用户名或密码错误');
            return false;
        }
        // 判断该账户登陆是否受到限制
        if (! kernel::single('pam_lock')->checkusername($userData['login_account'], $msg)) {
            return false;
        }
        
        $filter = array(
            'login_type' => $userData['login_type'],
            'login_account' => $userData['login_account']
        );
        $account = app::get('pam')->model('members')->getList('member_id,password_account,login_password,createtime', $filter);
        if (! $account) {
            $msg = $this->password_error($userData);
            return false;
        }
        $login_password = pam_encrypt::get_encrypted_password($userData['login_password'], 'member', array(
            'createtime' => $account[0]['createtime'],
            'login_name' => $account[0]['password_account']
        ));
        
        if ($account[0]['login_password'] != $login_password) {
            $msg = $this->password_error($userData);
            return false;
        }
        return $account[0]['member_id'];
    }
 // end function
    private function password_error($userData) {
        $left_times = kernel::single('pam_lock')->increase($userData['login_account']);
        $msg = app::get('pam')->_('用户名或密码错误');
        if ($left_times >= 7) {
            if ($left_times >= 10) {
                $msg = $msg . app::get('pam')->_("由于错误次数太多，账号已被锁定，请3小时后再次登录");
            } else {
                $msg = $msg . app::get('pam')->_(",剩余") . (10 - $left_times) . app::get('pam')->_("次机会");
            }
        }
        
        return $msg;
    }

    public function vcode_verify($vcode) {
        if (! base_vcode::verify('admin', $vcode)) {
            return false;
        }
        return true;
    }
}
